package com.tsing.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.log.Log;
import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.pojo.entity.AdChannel;
import com.tsing.service.ChannelService;
import com.tsing.service.UserService;
import com.tsing.service.impl.ChannelServiceImpl;
import com.tsing.service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/4 8:44
 */
public class ChannelController {
    private final ChannelService channelService = new ChannelServiceImpl();

    public PageResult<AdChannel> list(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;

        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;

//        修改status为null
        Integer status = (Integer) jsonObject.get("status");
        status = (status != null && status > 0) ? status : null;

        String name = jsonObject.getString("name");
        name = (name != null) ? name : "";

        //调用 channelService 获取 PageResult
        PageResult<AdChannel> pageResult = channelService.list(page, size, status, name);

        return pageResult;
    }

    public Result save(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        AdChannel adChannel = new AdChannel();
        adChannel.setName(jsonObject.getString("name"));
        adChannel.setDescription(jsonObject.getString("description"));
        adChannel.setStatus(jsonObject.getInteger("status"));
        adChannel.setOrd(Integer.parseInt(jsonObject.getString("ord")));

        int result = channelService.save(adChannel);
        if (result > 0) {
            return Result.success();
        } else {
            return Result.error("添加失败！");
        }

    }

    public Result update(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        AdChannel adChannel = new AdChannel();
        adChannel.setId(Long.parseLong(jsonObject.getString("id")));
        adChannel.setName(jsonObject.getString("name"));
        adChannel.setDescription(jsonObject.getString("description"));

        adChannel.setStatus(jsonObject.getBoolean("status") ? 1 : 0);

        Integer ord = (Integer) jsonObject.get("ord");
        adChannel.setOrd(ord);

        int result = channelService.update(adChannel);
        if (result > 0) {
            return Result.success();
        } else {
            return Result.error("更新失败！");
        }
    }

    public Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());


        Integer id = jsonObject.getInteger("id");
        int result = channelService.delete(id);
        if (result > 0) {
            return Result.success();
        } else {
            return Result.error("删除失败！");
        }
    }
}
